home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 120_01.zip / CHARIO.C < prev    next >
Text File  |  1993-06-01  |  4KB  |  176 lines

  1. /* HEADER: CUG120.04;
  2.    TITLE: CHARIO;
  3.    DATE: 00/00/1980;
  4.    DESCRIPTION: "Character oriented file I/O.";
  5.    KEYWORDS: random file input,character input/output;
  6.    FILENAME: CHARIO.C;
  7.    CRC: 0DB7;
  8.    AUTHORS: Scott W. Layson;
  9. */
  10. /* CHARIO.C        Character-oriented file I/O
  11.  
  12.     Written 1980 by Scott W. Layson
  13.     This code is in the public domain.
  14.  
  15. These routines deal with reading and writing large blocks of
  16. characters, when they do not fit neatly in sectors.  At the moment,
  17. only sequential output is supported; input is fully random. */
  18.  
  19.  
  20. #define TRUE (-1)
  21. #define FALSE 0
  22. #define NULL 0
  23.  
  24.  
  25. /* i/o buffer */
  26. struct iobuf {
  27.     int fd;
  28.     int isect;            /* currently buffered sector */
  29.     int nextc;            /* index of next char in buffer */
  30.     char buff [128];
  31.     };
  32.  
  33. /* seek opcodes */
  34. #define ABSOLUTE    0
  35. #define RELATIVE    1
  36.  
  37. #define INPUT        0
  38. #define OUTPUT        1
  39. #define UPDATE        2
  40.  
  41.  
  42. copen (buf, fname)            /* open a file for char input */
  43.     struct iobuf *buf;
  44.     char *fname;
  45. {
  46.     buf->fd = open (fname, INPUT);
  47.     if (buf->fd == -1) return (-1);
  48.     read (buf->fd, &buf->buff, 1);
  49.     buf->isect = 0;
  50.     buf->nextc = 0;
  51.     return (buf);
  52.     }
  53.  
  54.  
  55. ccreat (buf, fname)            /* create a file for char output */
  56.     struct iobuf *buf;
  57.     char *fname;
  58. {
  59.     buf->fd = creat (fname);
  60.     if (buf->fd == -1) return (-1);
  61.     buf->isect = 0;
  62.     buf->nextc = 0;
  63.     return (buf);
  64.     }
  65.  
  66.  
  67. cclose (buf)                /* close the file assoc. with buf */
  68.     struct iobuf *buf;
  69. {
  70.     close (buf->fd);
  71.     }
  72.  
  73.  
  74. cseek (buf, nbytes, code)    /* seek to a character (input only!) */
  75.     struct iobuf *buf;
  76.     unsigned nbytes, code;
  77. {
  78.     int newsect;
  79.  
  80.     if (buf < 0) return (-1);
  81.     if (code == RELATIVE) nbytes += buf->isect * 128 + buf->nextc;
  82.     newsect = nbytes / 128;
  83.     if (newsect != buf->isect
  84.         && (seek (buf->fd, newsect, ABSOLUTE) == -1
  85.            || read (buf->fd, &buf->buff, 1) == -1)) return (-1);
  86.     buf->nextc = nbytes % 128;
  87.     buf->isect = newsect;
  88.     return (nbytes);
  89.     }
  90.  
  91.  
  92. cread (buf, dest, nbytes)    /* read some bytes into dest */
  93.     struct iobuf *buf;
  94.     char *dest;
  95.     unsigned nbytes;
  96. {
  97.     int navail, nsects, nleft, nread1, nread2;
  98.  
  99.     if (buf < 0) return (-1);
  100.     navail = umin (nbytes, 128 - buf->nextc);
  101.     movmem (&buf->buff[buf->nextc], dest, navail);
  102.     nbytes -= navail;
  103.     buf->nextc += navail;
  104.     dest += navail;
  105.     nsects = nbytes / 128;
  106.     if (nsects) {
  107.         nread1 = read (buf->fd, dest, nsects);
  108.         if (nread1 == -1) return (navail);
  109.         buf->isect += nread1;
  110.         if (nread1 < nsects) return (navail + nread1 * 128);
  111.         dest += nread1 * 128;
  112.         }
  113.     else nread1 = 0;
  114.     if (buf->nextc == 128) {
  115.         nread2 = read (buf->fd, &buf->buff, 1);
  116.         if (nread2 == -1) return (navail);
  117.         ++(buf->isect);
  118.         buf->nextc = 0;
  119.         if (nread2 < 1) return (navail + nread1 * 128);
  120.         }
  121.     nleft = nbytes % 128;
  122.     movmem (&buf->buff, dest, nleft);
  123.     buf->nextc += nleft;
  124.     return (navail + nbytes);
  125.     }
  126.  
  127.  
  128. cwrite (buf, source, nbytes)    /* write some bytes from source */
  129.     struct iobuf *buf;
  130.     char *source;
  131.     unsigned nbytes;
  132. {
  133.     unsigned nleft, nfill, nsects;
  134.     
  135.     if (buf < 0) return (-1);
  136.     if (buf->nextc) {
  137.         nfill = umin (nbytes, 128 - buf->nextc);
  138.         movmem (source, &buf->buff[buf->nextc], nfill);
  139.         buf->nextc += nfill;
  140.         nbytes -= nfill;
  141.         source += nfill;
  142.         }
  143.     if (buf->nextc == 128) {
  144.         ++(buf->isect);
  145.         buf->nextc = 0;
  146.         if (write (buf->fd, &buf->buff, 1) < 1) return (-1);
  147.         }
  148.     nsects = nbytes / 128;
  149.     if (nsects  &&  write (buf->fd, source, nsects) < nsects)
  150.         return (-1);
  151.     nbytes %= 128;
  152.     movmem (source + nsects * 128, &buf->buff, nbytes);
  153.     buf->nextc += nbytes;
  154.     return (nsects * 128 + nbytes);
  155.     }
  156.  
  157.  
  158. cflush (buf)                /* flush an output buffer */
  159.     struct iobuf *buf;
  160. {
  161.     if (buf->nextc  &&  write (buf->fd, &buf->buff, 1) < 1)
  162.         return (-1);
  163.     return (1);
  164.     }
  165.  
  166.  
  167. umin (a, b)                /* unsigned min */
  168.     unsigned a, b;
  169. {
  170.     return ((a < b) ? a : b);
  171.     }
  172.  
  173.  
  174. /* End of CHARIO.C  --  Character oriented file I/O */
  175.     buf->nextc = nbytes % 128;
  176.     buf->isect = newsec